﻿
using System;
using InDbContext;
using System.Linq;
using System.Data.Entity.SqlServer;

namespace In.Api.Helpers
{
    public static class HospWsLogHelper
    {
        public static void Log(string type, string request, string response)
        {
            //写日志
            try
            {
                using (var ctx = new HospDatabaseEntity())
                {
                    var log = new Log
                    {
                        Action = type,
                        Request = request,
                        Response = response,
                        CreateDT = DateTime.Now
                    };
                    ctx.Logs.Add(log);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            { }
        }

        public static void Notify(string type, string request, string response)
        {
            //写日志
            try
            {
                using (var ctx = new HospDatabaseEntity())
                {
                    var notify = new Notify
                    {
                        NotifyType = type,
                        Request = request,
                        Response = response,
                        CreateDT = DateTime.Now
                    };
                    ctx.Notifies.Add(notify);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            { }
        }

        public static string GetBillInfo(In.Api.Dto.Notify.Notification.BillInfo billinfo,out string orderId)
        {
            var codeUrl = "";
            orderId = "";
            //写日志
            try
            {
                using (var ctx = new HospDatabaseEntity())
                {
                     var bill = ctx.BillInfoes.Where(x =>
                          x.CardNo == billinfo.CardNo &&
                          x.CardType == billinfo.CardType &&
                          x.ClinicNo == billinfo.ClinicNo &&
                          x.DeptId == billinfo.DeptId &&
                          x.DeptName == billinfo.DeptName &&
                          x.DoctorId == billinfo.DoctorId &&
                          x.DoctorName == billinfo.DoctorName &&
                          x.OrderDate == billinfo.OrderDate &&
                          x.FeeTypeId == billinfo.FeeTypeId &&
                          x.FeeTypeName == billinfo.FeeTypeName &&
                          x.InsuranceFee == billinfo.InsuranceFee &&
                          x.IsSelfIdentity == billinfo.IsSelfIdentity &&
                          x.PatientId == billinfo.PatientId &&
                          x.PersonalFee == billinfo.PersonalFee &&
                          x.PatientName == billinfo.PatientName &&
                          x.SumFee == billinfo.SumFee &&
                          SqlFunctions.DateDiff("MINUTE", x.CreateDT, SqlFunctions.GetDate()) <= 3 &&
                          x.CodeUrl != "").FirstOrDefault();
                    if (bill != null)
                    {
                        bill.Count = (bill.Count ?? 1) + 1;
                        ctx.SaveChanges();
                        codeUrl = bill.CodeUrl;
                        orderId = bill.HospitalId;
                    }
                }
            }
            catch (Exception ex)
            { }
            return  codeUrl;
        }

        public static void SaveBillInfo(In.Api.Dto.Notify.Notification.BillInfo billinfo, string codeUrl,string orderId)
        {
            //写日志
            try
            {
                using (var ctx = new HospDatabaseEntity())
                {
                    var bill = new BillInfo
                    {
                        CreateDT = DateTime.Now,
                        CardNo = billinfo.CardNo,
                        CardType = billinfo.CardType,
                        ClinicNo = billinfo.ClinicNo,
                        CodeUrl = codeUrl,
                        Count = 1,
                        DeptId = billinfo.DeptId,
                        DeptName = billinfo.DeptName,
                        DoctorId = billinfo.DoctorId,
                        DoctorName = billinfo.DoctorName,
                        OrderDate = billinfo.OrderDate,
                        FeeTypeId = billinfo.FeeTypeId,
                        FeeTypeName = billinfo.FeeTypeName,
                        HospitalId = orderId,

                        InsuranceFee = billinfo.InsuranceFee,
                        IsSelfIdentity = billinfo.IsSelfIdentity,
                        PatientId = billinfo.PatientId,
                        PersonalFee = billinfo.PersonalFee,
                        PatientName = billinfo.PatientName,
                        SumFee = billinfo.SumFee,

                    };
                    ctx.BillInfoes.Add(bill);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            { }
        }

        public static string GetPayInfo(In.Api.Dto.Notify.Notification.PayInfo payinfo)
        {
            var rescode = "";
            //写日志
            try
            {
                using (var ctx = new HospDatabaseEntity())
                {
                    var pay = ctx.PayInfoes.Where(x =>

                          x.ClinicNo == payinfo.ClinicNo &&

                          x.OrderDate == payinfo.OrderDate &&

                          x.PatientId == payinfo.PatientId &&

                          x.SumFee == payinfo.SumFee).FirstOrDefault();
                    if (pay != null)
                    {
                        pay.Count = (pay.Count ?? 1) + 1;
                        ctx.SaveChanges();
                        rescode = "0";
                    }
                }
            }
            catch (Exception ex)
            { }
            return rescode;
        }

        public static void SavePayInfo(In.Api.Dto.Notify.Notification.PayInfo payinfo)
        {
            //写日志
            try
            {
                using (var ctx = new HospDatabaseEntity())
                {
                    var bill = new PayInfo
                    {
                        CreateDT = DateTime.Now,
                        ClinicNo = payinfo.ClinicNo,
                        Count = 1,
                        OrderDate = payinfo.OrderDate,
                        PatientId = payinfo.PatientId,
                        SumFee = payinfo.SumFee,

                    };
                    ctx.PayInfoes.Add(bill);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            { }
        }
    }
}